package com.ruoyi.product.service.impl;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.GoodsDirectoryMapper;
import com.ruoyi.product.domain.GoodsDirectory;
import com.ruoyi.product.service.IGoodsDirectoryService;

/**
 * 店铺目录Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-29
 */
@Service
public class GoodsDirectoryServiceImpl implements IGoodsDirectoryService 
{
    @Autowired
    private GoodsDirectoryMapper goodsDirectoryMapper;

    /**
     * 查询店铺目录
     * 
     * @param id 店铺目录ID
     * @return 店铺目录
     */
    @Override
    public GoodsDirectory selectGoodsDirectoryById(Long id)
    {
        return goodsDirectoryMapper.selectGoodsDirectoryById(id);
    }

    /**
     * 查询店铺目录列表
     * 
     * @param goodsDirectory 店铺目录
     * @return 店铺目录
     */
    @Override
    public List<GoodsDirectory> selectGoodsDirectoryList(GoodsDirectory goodsDirectory)
    {
        return goodsDirectoryMapper.selectGoodsDirectoryList(goodsDirectory);
    }

    /**
     * 新增店铺目录
     * 
     * @param goodsDirectory 店铺目录
     * @return 结果
     */
    @Override
    public int insertGoodsDirectory(GoodsDirectory goodsDirectory)
    {
        goodsDirectory.setCreateTime(DateUtils.getNowDate());
        return goodsDirectoryMapper.insertGoodsDirectory(goodsDirectory);
    }

    /**
     * 修改店铺目录
     * 
     * @param goodsDirectory 店铺目录
     * @return 结果
     */
    @Override
    public int updateGoodsDirectory(GoodsDirectory goodsDirectory)
    {
        goodsDirectory.setUpdateTime(DateUtils.getNowDate());
        return goodsDirectoryMapper.updateGoodsDirectory(goodsDirectory);
    }

    /**
     * 批量删除店铺目录
     * 
     * @param ids 需要删除的店铺目录ID
     * @return 结果
     */
    @Override
    public int deleteGoodsDirectoryByIds(Long[] ids)
    {
        return goodsDirectoryMapper.deleteGoodsDirectoryByIds(ids);
    }

    /**
     * 删除店铺目录信息
     * 
     * @param id 店铺目录ID
     * @return 结果
     */
    @Override
    public int deleteGoodsDirectoryById(Long id)
    {
        return goodsDirectoryMapper.deleteGoodsDirectoryById(id);
    }

   /**
    * 构建前端所需要树结构
    * 
    * @param goodsDirectorys 部门列表
    * @return 树结构列表
    */
    @Override
    public List<GoodsDirectory> buildGoodsDirectoryTree(List<GoodsDirectory> goodsDirectorys)
    {
        List<GoodsDirectory> returnList = new ArrayList<GoodsDirectory>();
        List<Long> tempList = new ArrayList<Long>();
        for (GoodsDirectory goodsDirectory : goodsDirectorys)
        {
            
            tempList.add(goodsDirectory.getId());
        }
        for (Iterator<GoodsDirectory> iterator = goodsDirectorys.iterator(); iterator.hasNext();)
        {
            GoodsDirectory goodsDirectory = (GoodsDirectory) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(goodsDirectory.getParentId()))
            {
                recursionFn(goodsDirectorys, goodsDirectory);
                returnList.add(goodsDirectory);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = goodsDirectorys;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     * 
     * @param goodsDirectorys 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildGoodsDirectoryTreeSelect(List<GoodsDirectory> goodsDirectorys)
    {
        List<GoodsDirectory> goodsDirectoryTrees = buildGoodsDirectoryTree(goodsDirectorys);
        return goodsDirectoryTrees.stream().map(this::toTreeSelect).collect(Collectors.toList());
    }
    protected TreeSelect toTreeSelect(GoodsDirectory object){
        TreeSelect result= new TreeSelect();
        result.setId(object.getId());
        result.setLabel(object.getName());
        List<GoodsDirectory>childeren=   (List<GoodsDirectory>)object.getChildren();
        result.setChildren(childeren.stream().map(this::toTreeSelect).collect(Collectors.toList()));
        return result;
    }
    /**
     * 递归列表
     */
    private void recursionFn(List<GoodsDirectory> list, GoodsDirectory t)
    {
        // 得到子节点列表
        List<GoodsDirectory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (GoodsDirectory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
   * 得到子节点列表
   */
    private List<GoodsDirectory> getChildList(List<GoodsDirectory> list, GoodsDirectory t)
    {
        List<GoodsDirectory> tlist = new ArrayList<GoodsDirectory>();
        Iterator<GoodsDirectory> it = list.iterator();
        while (it.hasNext())
        {
            GoodsDirectory n = (GoodsDirectory) it.next();
            
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
   * 判断是否有子节点
   */
    private boolean hasChild(List<GoodsDirectory> list, GoodsDirectory t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}
